@extends('Admin.base')
@section('css')

@endsection
@section('style')

@endsection
@section('content')
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item layui-row">

            </div>
        </div>
        <div class="layui-card-body">
            <table id="LAY-table" lay-filter="LAY-table"></table>

            <script type="text/html" id="tableToolbar">
                <a href="/admin/activity/edit/@{{ d.id }}" class="layui-btn layui-btn-green layui-btn-xs">
                    编辑
                </a>

                @{{# if(d.status == 1 ){ }}
                    <a class="layui-btn layui-btn-xs layui-btn-danger"  lay-event="updataStatus">
                        关闭
                    </a>
                @{{# }else  { }}
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">
                        删除
                    </a>
                    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="updataStatus">
                        开启
                    </a>
                @{{# } }}


            </script>


            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <a class="layui-btn" href="{{ route('activity.add') }}">添加活动</a>
                </div>
            </script>
        </div>
    </div>
@endsection

@section('script')
    <script src="/js/ajax.js"></script>
    <script>
        layui.use(['form', 'table', 'laydate'], function(){
            var $ = layui.$
                ,form = layui.form
                ,table = layui.table

            table.render({
                elem: '#LAY-table'
                ,toolbar: '#toolbarDemo'
                ,defaultToolbar: []
                ,url: "{{url('/admin/activity/table')}}"
                ,method: "post"
                ,page: true //开启分页
                ,limit:10
                ,cols: [[
                    {field:'activity_name', title:'活动名称' }
                    ,{field: 'status', title: '状态' ,templet:function(d){
                        return d.status == 1 ? "开启" : "关闭";
                    }}
                    ,{fixed: 'right', title: '操作' , width: 200, align:'center', toolbar: '#tableToolbar'}
                ]]

            });

            //监听工具条
            table.on('tool(LAY-table)', function(obj){
                var data = obj.data;
                console.log(obj.event)
                if(obj.event === 'del'){
                    layer.confirm('确认删除行吗?', function(index){
                        $.ajax({
                            url: '/admin/activity/destroy/'+ data.id
                            , type: 'POST'
                            , dataType: 'json'
                            , data: {'_method':"DELETE"}
                            , success: function (ret) {
                                if (ret.hasOwnProperty('code') && ret.code == 200) {
                                    layer.msg(ret.message,{icon: 1,time:2000});
                                    obj.del();
                                    layer.close(index);
                                    return false;
                                }
                                layer.msg(ret.message,{icon:2});
                            }
                        });

                    });
                }else if(obj.event === 'updataStatus'){
                    layer.confirm('确认修改状态吗?', function(index){
                        $.ajax({
                            url: '/admin/activity/upstatus/'+ data.id
                            , type: 'POST'
                            , dataType: 'json'
                            , data: {'status': data.status }
                            , success: function (ret) {
                                if (ret.hasOwnProperty('code') && ret.code == 200) {
                                    layer.msg(ret.message,{icon: 1,time:2000});
                                    table.reload('LAY-table');
                                    return false;
                                }
                                layer.msg(ret.message,{icon:2});
                            }
                        });
                    });
                }
            });
            //监听搜索
            form.on('submit(LAY-search)', function(data){
                var field = data.field;
                //执行重载
                table.reload('LAY-table', {
                    where: field,page: {curr: 1}
                });
            });
        });
    </script>

@endsection